// xc_lshop/index/com/activity.js
var actbill = require('../../../xc/model/activity_list')
// var actprobill = require('../../../xc/model/pro_contents')
var actbillclass = require('../../../xc/model/pro_class')
const app = getApp()

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    themcolor: Object,
    mode: {
      type: String,
      value: "num", //num 固定数量，list 列表
    },
    activityid: {
      type: String,
      value: -1,
      observer: function (value) {
        if (value > 0) {
          if (this.data.mode == 'list') {
            console.log("ffffffffff")
            var that = this
            this.getpagelist(that, 1)
          } else {
            console.log("yyyyyyyyyyyyyy")

            this.getmodel(value)
          }
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    classid:0,

    selected: {},
    taskname: "购买值",
    CustomBar: app.globalData.CustomBar,
  },
  ready() {
    var app = getApp();



  },
  /**
   * 组件的方法列表
   */
  methods: {
    onclicknav(e){
      var id=e.currentTarget.dataset.id;
      this.setData({
        classid:id
      })

      this.getpro(id)
    },
    getpro(classid){
      var activity=this.data.activity;
      var obj = JSON.parse(JSON.stringify(activity.pros))
      var modellist=[]
      if(classid==0){
        modellist=obj
      }else{
        for(var i in obj){
          if(obj[i]['bpid']==classid){
            modellist.push(obj[i])
          }
        }
      }

      this.setData({
        modellist:modellist
      })
      // console.log(this.data.CustomBar)
      wx.pageScrollTo({
        scrollTop: 46,
        duration: 300
      })
      
     
    },
    proclass() {
      var that = this
      var strwhere = {}
      strwhere["status"] = 1
      //加载全部分类
      actbillclass.apiall().then(item => {
        that.setData({
          treeclass: item.obj,
          actpidx: 0
        })

        if (that.data.actpid) {
          that.getactpidx(that.data.actpid)
        }
        console.log(that.data.actspid)
        if (that.data.actspid > 0) {
          if (that.data.navtype == 'left') {
            console.log(that.data.actspid)
            that.selectComponent("#protopnav").setactcid(that.data.actspid)
          } else {
            that.selectComponent("#imagenav").setactcid(that.data.actspid)
          }
        }

      })
    },
    init(aid) {
      this.setData({
        activityid: aid
      })
  
      var app = getApp();
      if (app.globalData.configdata && app.globalData.configdata.task && app.globalData.configdata.task.taskname) {
        this.setData({
          taskname: app.globalData.configdata.task.taskname
        })
      }

      this.proclass()

    },
    buyop: function (ev) {
      var item = {
        proid: ev.currentTarget.dataset.id,
        activity: this.data.activity
      }
   

      this.triggerEvent("buyactvitypro", item)


    },
    //列表
    getpagelist(that, rest, length = 15) {

      if (rest == 1) {
        that.getmodel(that.data.activityid)
        that.setData({
          rest: rest
        })
      } else {
        this.setData({
          isload: true
        })
        var obj = that.data.activity.pros

        var updatemdoel = {};

        // console.log("tttttttttttttttttttttttt")
        // console.log((rest -1) * length)
        // console.log(length * rest )
        // console.log(obj)

        var nmodellist = obj.splice((rest - 1) * length, length * rest);
        // console.log("tttttttttttttttttttttttt")
        // console.log(nmodellist)

        updatemdoel["modellist"] = that.data.modellist.concat(nmodellist);
        // console.log("tttttttttttttttttttttttt")
        // console.log(updatemdoel)

        // }
        updatemdoel["isload"] = false
        that.setData(updatemdoel);
      }
      return;
    },
    getmodel(id) {
      var that = this;
      actbill.getmodel(id).then(function (res) {
        if (res['status'] == 1) {
          var obj = JSON.parse(JSON.stringify(res['obj'].pros))
          that.setData({
            modellist: obj,
            activity: res['obj']
          })
        }
      })
    },
    onReachBottom() {
      var that = this;
      // that.getpagelist(that, that.data.rest + 1)
    }
  }
})